package docsim;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

public class WordCount implements WritableComparable{
	Text word;
	int count;
	
	public void set(Text w, int c) {
		word = w;
		count = c;
	}

	@Override
	public void readFields(DataInput arg0) throws IOException {
		word.readFields(arg0);
		count = arg0.readInt();
	}

	@Override
	public void write(DataOutput arg0) throws IOException {
		word.write(arg0);
		arg0.writeLong(count);
	}

	@Override
	public int hashCode() {
		return (31 * word.toString().hashCode()) ^ count;
	}
	
	@Override
	public boolean equals(Object obj) {
		return (((WordCount)obj).word.toString().equals(word.toString()) && (((WordCount)obj).count == count)); 
	}
	
	@Override
	public int compareTo(Object o) {
		if(word.toString().equals(((WordCount)o).word.toString()))
		{
			return (((WordCount)o).count - count);
		}
		else
		{
			return (((WordCount)o).word.toString().compareTo(word.toString()));
		}
	}	
}
